package 
{
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.KeyboardEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import helpers.Layers;
	import ru.volgogradetzzz.net.SequentialSwfLoader;
	import ru.volgogradetzzz.utils.Console;
	import ru.volgogradetzzz.utils.Logger;
	
	/**
	 * ...
	 * @author Volgogradetzzz
	 */
	public class Main extends Sprite 
	{
		private const _SWF_LOADER:SequentialSwfLoader = SequentialSwfLoader.getInstance();
		private const _LOGGER:Logger = Logger.getInstance();
		private const _CONSOLE:Console = Console.getInstance();
		
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(event:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			_CONSOLE.registerCommand(showLog, '-showLog', 'Выводит на экран Logger.');
			_CONSOLE.registerCommand(hideLog, '-hideLog', 'Убирает Logger с экрана.');
			
			addChild(Layers.LAYER_CONSOLE);
			addChild(Layers.LAYER_LOGGER);
			
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
			loader.addEventListener(Event.COMPLETE, completeHandler);
			
			stage.addEventListener(KeyboardEvent.KEY_DOWN, stageKeyDownHandler);
			
			_SWF_LOADER.addEventListener(ErrorEvent.ERROR, swfLoaderErrorHandler);
			
			_LOGGER.log('config.xml loading start');
			loader.load(new URLRequest('../src/config/config.xml'));
		}
		
		private function swfLoaderErrorHandler(event:ErrorEvent):void 
		{
			//TODO
		}
		
		private function stageKeyDownHandler(event:KeyboardEvent):void 
		{
			if (event.keyCode == 192)
			{
				Layers.LAYER_CONSOLE.contains(_CONSOLE) ? Layers.LAYER_CONSOLE.removeChild(_CONSOLE) : Layers.LAYER_CONSOLE.addChild(_CONSOLE);
			}
		}
		
		private function showLog():void
		{
			if(!Layers.LAYER_LOGGER.contains(_LOGGER)) Layers.LAYER_LOGGER.addChild(_LOGGER);
		}
		
		private function hideLog():void
		{
			if(Layers.LAYER_LOGGER.contains(_LOGGER)) Layers.LAYER_LOGGER.removeChild(_LOGGER);
		}
		
		private function ioErrorHandler(event:IOErrorEvent):void 
		{
			_LOGGER.log('config.xml ioError');
		}
		
		private function securityErrorHandler(event:SecurityErrorEvent):void 
		{
			_LOGGER.log('config.xml securityError');
		}
		
		private function completeHandler(event:Event):void 
		{
			_LOGGER.log('config.xml complete');
			
			var loader:URLLoader = event.target as URLLoader;
			var xml:XML = XML(loader.data);
			
			_LOGGER.log('config.xml:\n' + xml, Logger.INFO);
			
			_SWF_LOADER.addEventListener(Event.COMPLETE, preloadAssetsLoadHandler);
			
			for each(var asset:XML in xml.preloadAssets)
			{
				_SWF_LOADER.load(asset.asset.@id, asset.asset.@path);
			}
		}
		
		private function preloadAssetsLoadHandler(event:Event):void 
		{
			_SWF_LOADER.removeEventListener(Event.COMPLETE, preloadAssetsLoadHandler);
			
			trace('AAA')
		}
	}
}